layui.use(['table', 'admin', 'ax', 'laydate'], function () {
    var $ = layui.$;
    var table = layui.table;
    var $ax = layui.ax;
    var admin = layui.admin;
    var laydate = layui.laydate;

    /**
     * 商品表管理
     */
    var BasicGoods = {
        tableId: "basicGoodsTable"
    };

    /**
     * 初始化表格的列
     */
    BasicGoods.initColumn = function () {
        return [[
            {type: 'checkbox'},
            {field: 'id', hide: true, title: 'ID'},
            {field: 'goodsCustomerName', title: 'Customer', width: 130, style: 'background-color: #ecf5ff;font-weight: 600;'},
            {field: 'goodsName', sort: true, title: 'Product Name', width: 200, style: 'font-weight: 600;'},
            {field: 'goodsNumber', sort: true, title: 'Product Code', width: 130, style: 'font-weight: 600;'},
            {field: 'goodsNo', sort: true, title: 'Item Number', width: 130, style: 'font-weight: 600;'},
            {field: 'goodsSupportvolume', title: 'Quantity per Pallet', style: 'font-weight: 600;'},
            {field: 'goodsVolume', title: 'Volume'},
            {field: 'goodsWeight', title: 'Weight'},
            {field: 'goodsCharging', title: 'Billing Price'},
            {field: 'goodsBigtype', title: 'Category'},
            {field: 'goodsType', title: 'Type'},
            {field: 'goodsUnit', title: 'Unit'},
            {field: 'goodsPacking', title: 'Packaging'},
            {field: 'goodsSpecs', title: 'Specifications'},
            {align: 'center', width: 70, toolbar: '#tableBar', title: 'Operation', fixed: 'right'}
        ]];
    };

    /**
     * 点击查询按钮
     */
    BasicGoods.search = function () {
        var queryData = {};
        queryData['condition'] = $("#condition").val();
        table.reload(BasicGoods.tableId, {
            where: queryData, page: {curr: 1},
             done:function () {
            }
        });
    };

    // 渲染表格
    var tableResult = table.render({
        elem: '#' + BasicGoods.tableId,
        url: Feng.ctxPath + '/orderPutDetailed/ChoiceGoods',
        page: true,
        limit: 30,
        height: "full-60",
        cellMinWidth: 100,
        cols: BasicGoods.initColumn()
    });

    // 搜索按钮点击事件
    $('#btnSearch').click(function () {
        BasicGoods.search();
    });
    
    // 批量分配物料大类编码
    $('#btnAssign').click(function () {
    	var checkStatus = table.checkStatus('basicGoodsTable');
        data = checkStatus.data;
        if (data.length>0) {
            var goodsNumbers = '';
            for (var i = 0; i < data.length; i++) {
            	goodsNumbers+=data[i].goodsNumber+";";
			}
	    	parent.OrderPutDetailedInfoDlg.data.goodsNumber = goodsNumbers;//商品编号
	    	var Index = parent.layer.getFrameIndex(window.name);
	        parent.layer.close(Index);
		} else {
			layer.alert('Please select at least one record');
		}
    });
    
    //回填商品数据
    function confirm(data){
    	parent.OrderPutDetailedInfoDlg.data.goodsNumber = data.goodsNumber+";";//商品编号
    	var Index = parent.layer.getFrameIndex(window.name);
        parent.layer.close(Index);
    }
    
    // 双击行事件
    table.on('rowDouble(' + BasicGoods.tableId + ')', function (obj) {
        var data = obj.data;
    	confirm(data);
    });

    // 工具条点击事件
    table.on('tool(' + BasicGoods.tableId + ')', function (obj) {
        var data = obj.data;
        var layEvent = obj.event;
        if (layEvent === 'confirm') {
        	confirm(data);
        } 
    });
});
